// pages/confirmOrder/confirmOrder.js
var http = require('../../utils/http.js');
var config = require("../../utils/config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo: {},
    userAddr:null,
    remarks:""
  },

  onRemarksInput:function(e){
    this.setData({
      remarks: e.detail.value
    });
  },

  //加载订单数据
  loadOrderData: function () {
    var addrId = 0;
    if (this.data.userAddr != null) {
      addrId = this.data.userAddr.addrId;
    }
    wx.showLoading({
      mask: true
    });
    let groupOrderItem = JSON.parse(wx.getStorageSync("groupOrderItem"))
    var params = {
      url: "/p/group/order/confirm",
      method: "POST",
      data: {
        addrId: addrId,
        groupSkuId: groupOrderItem.groupSkuId ,
        groupTeamId: groupOrderItem.groupTeamId ,
        prodCount: groupOrderItem.prodCount
      },
      callBack: res => {
        console.log(res)
        wx.hideLoading();
        this.setData({
          orderInfo: res,
          item: res.userAddrDto
        });
      },
      errCallBack: res => {
        wx.hideLoading();
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: res.data,
          success: res => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    };
    http.request(params);

  },

  // 提交订单
  commitOrder() {
    wx.showLoading()
    var orderShopParam = {
      remarks: this.data.remarks,
    }
    http.request({
      url: `/p/group/order/submit`,
      method: 'POST',
      data: orderShopParam,
      callBack: (res) => {
        this.calWeixinPay(res.orderNumbers)
      }
    })
  },
  /**
   * 唤起微信支付
   */
  calWeixinPay: function (orderNumbers) {
    wx.showLoading({
      mask: true
    });
    var params = {
      url: "/p/order/pay",
      method: "POST",
      data: {
        payType: 1,
        orderType:1,
        orderNumbers: orderNumbers
      },
      callBack:  (res)=> {
        wx.hideLoading();
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.packageValue,
          signType: res.signType,
          paySign: res.paySign,
          success: e => {
            // console.log("支付成功");
            wx.showModal({
              title: '支付成功',
              content: '',
              showCancel:false,
              success:()=>{
                wx.navigateTo({
                  url: '/pages/spellGroupDetails/spellGroupDetails?orderNumber=' + orderNumbers,
                })
              }
            })
            
          },
          fail: err => {
            wx.navigateTo({
              url: '/pages/pay-result/pay-result?sts=0&orderNumbers=' + orderNumbers + "&orderType=" + this.data.orderType,
            })
          }
        })

      }
    };
    http.request(params);
  },

  /**
   * 去地址页面
   */
  toAddrListPage: function () {
    wx.navigateTo({
      url: '/pages/delivery-address/delivery-address?order=0',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    if (currPage.data.selAddress == "yes") {
      this.setData({ //将携带的参数赋值
        userAddr: currPage.data.item
      });
    }
    this.loadOrderData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})